package com.tiancheng.trade.order.feign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tiancheng.trade.commom.web.http.PlatformRequest;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.order.bo.CreditOrderPayQueryBo;
import com.tiancheng.trade.order.bo.CreditOrderQueryDetailBo;
import com.tiancheng.trade.order.util.MerchantInfoConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 信用系统服务调用
 *
 * @author likailun
 * @date 2024/09/16
 */
@Component
@Slf4j
public class CreditSystemService {

    @Value(value = "${system.credit.domain}")
    private String creditDomain;
    @Resource
    private PlatformRequest platformRequest;
    @Resource
    private MerchantInfoConfig merchantInfoConfig;

    /**
     * 获取优惠券信息
     *
     * @param params
     * @return
     */
    public String freeze(Map<String, Object> params) {
        try {
            log.info("/zhima/freeze params" + JSON.toJSONString(params));
            Result result = platformRequest.jsonPostRequest(creditDomain + "/zhima/freeze", null,
                    params, this.getTokenHeader());
            Map<String, Object> resultMap = JsonUtil.fromJson(result.toString(), Map.class);
            LinkedHashMap<String,Object> data = (LinkedHashMap<String, Object>) resultMap.get("data");
            if(data.containsKey("body")){
                return data.get("body").toString();
            }
            return null;
        } catch (Exception e) {
            log.error("线上资金授权冻结失败  meessage:{}",e.getMessage());
            throw e;
        }
    }

    /**
     * 解冻
     *
     * @param params
     * @return
     */
    public Result unfreeze(Map<String, Object> params) {
        try {
            log.info("/zhima/unfreeze params" + JSON.toJSONString(params));
            return platformRequest.jsonPostRequest(creditDomain + "/zhima/unfreeze", null,
                    params,  this.getTokenHeader());

        } catch (Exception e) {
            log.error("线上资金授权冻结失败 meessage:{}",e.getMessage());
            throw e;
        }

    }

    /**
     * 资金授权操作查询接口
     *
     * @param requestNo
     * @param gwOrderId
     * @param merchantId
     * @return
     */
    public CreditOrderQueryDetailBo detailQuery(String requestNo,String gwOrderId,String merchantId) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("requestNo",requestNo);
            param.put("gwOrderId",gwOrderId);
            param.put("merchantId",merchantInfoConfig.getCreditSystemMerchantCode(merchantId));
            log.info("/zhima/detailQuery params" + JSON.toJSONString(param));
//            return MockCreditOrderRollbackData.detailQuery(params);
            Result result = platformRequest.jsonPostRequest(creditDomain + "/zhima/detailQuery", null,
                    param,  this.getTokenHeader());
            Map<String, Object> resultMap = JsonUtil.fromJson(result.toString(), Map.class);

            return JSON.toJavaObject(JSON.parseObject(JSON.toJSONString(resultMap.get("data"))),CreditOrderQueryDetailBo.class);
        } catch (Exception e) {
            log.error("资金授权操作查询接口查询失败 meessage:{}",e.getMessage());
            throw e;
        }

    }

    /**
     * 授权转支付
     * @param params
     */
    public Object tradePay(Map<String, Object> params) {
        try {
            log.info("/zhima/tradePay params:" + JSON.toJSONString(params));
            Result result = platformRequest.jsonPostRequest(creditDomain + "/zhima/tradePay",null,
                    params, this.getTokenHeader());
            Map<String, Object> resultMap = JsonUtil.fromJson(result.toString(), Map.class);
            return resultMap.get("data");
        } catch (Exception e) {
            log.error("调用授权转支付失败 meessage:{}",e.getMessage());
            throw e;
        }
    }


    /**
     * 获取支付信息
     *
     * @param params
     * @return
     */
    public CreditOrderPayQueryBo tradePayQuery(Map<String, Object> params) {
        try {
            log.info("/zhima/tradePayQuery params" + JSON.toJSONString(params));
            Result result = platformRequest.jsonPostRequest(creditDomain + "/zhima/tradePayQuery",null,
                    params,  this.getTokenHeader());
            Map<String, Object> resultMap = JsonUtil.fromJson(result.toString(), Map.class);
            Object body = resultMap.get("data");
            if(body==null){
                return null;
            }
            return JSONObject.parseObject(JSON.toJSONString(body), CreditOrderPayQueryBo.class);
        } catch (Exception e) {
            log.error("获取授权转支付信息失败 code:{}； meessage:{}",e.getMessage());
            throw e;
        }

    }

    /**
     * 预授权订单取消
     *
     * @param params
     * @return
     */
    public void cancel(Map<String, Object> params) {
        try {
            log.info("/zhima/cancel params" + JSON.toJSONString(params));
            Result result = platformRequest.jsonPostRequest(creditDomain + "/zhima/cancel", null,
                    params,this.getTokenHeader());
        } catch (Exception e) {
            log.error("预授权订单取消失败  meessage:{}",e.getMessage());
            throw e;
        }
    }


    /**
     * 获取优惠券服务的token
     *
     * @return
     */
    private Header getTokenHeader()  {
        String token = platformRequest.getAccessToken();
        return new BasicHeader("token", token);
    }


}
